from rest_framework import viewsets
from rest_framework.response import Response
from .models import TripleJumpRecord
from .serializers import TripleJumpRecordSerializer
from .pagination import CustomPageNumberPagination  # 导入自定义分页类

class TripleJumpRecordViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = TripleJumpRecord.objects.all().order_by('id')  # 确保查询集是有序的
    serializer_class = TripleJumpRecordSerializer
    pagination_class = CustomPageNumberPagination  # 使用自定义分页类

    def list(self, request, *args, **kwargs):
        response = super().list(request, *args, **kwargs)
        return Response({
            'results': response.data['results'],
            'total_pages': response.data['total_pages'],
            'current_page': response.data['current_page'],
            'total_records': response.data['total_records'],
        })

    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)

    def update(self, request, *args, **kwargs):
        partial = kwargs.pop('partial', False)
        instance = self.get_object()
        serializer = self.get_serializer(instance, data=request.data, partial=partial)
        serializer.is_valid(raise_exception=True)
        self.perform_update(serializer)
        return Response(serializer.data)

    def destroy(self, request, *args, **kwargs):
        instance = self.get_object()
        self.perform_destroy(instance)
        return Response(status=status.HTTP_204_NO_CONTENT)